package com.uenpay.im.ui.old.activity.stickers;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 获取表情列表
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetStickersListInvokItem extends HttpInvokeItem {

	public class GetStickersListInvokItemResult {
		public int status;
		public String msg;
		public ArrayList<StickerEntity> arrayList = new ArrayList<StickerEntity>();
	}

	public GetStickersListInvokItemResult getOutput() {
		return (GetStickersListInvokItemResult) GetResultObject();
	}

	public GetStickersListInvokItem() {
		String url = APIConfiguration.getBaseUrl() + "Chat/Chat/myFaceList?"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		GetStickersListInvokItemResult result = new GetStickersListInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject jsonData = json.optJSONObject("data");
			if (jsonData != null && jsonData.has("data")) {
				JSONArray jsonArr = jsonData.optJSONArray("data");
				if (jsonArr != null && jsonArr.length() > 0) {
					for (int i = 0, size = jsonArr.length(); i < size; i++) {
						result.arrayList.add(desCustomEntity(jsonArr
								.optJSONObject(i)));
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

	private StickerEntity desCustomEntity(JSONObject json) {
		StickerEntity mCustomEntity = new StickerEntity();
		if(json.has("img")){
			JSONObject jsonObject = json.optJSONObject("img");
			mCustomEntity.fileHash = jsonObject.optString("filehash");
			mCustomEntity.fileId = jsonObject.optString("fileid");
		}
		mCustomEntity.favoriteid = json.optString("favoriteid");
		mCustomEntity.customeFaceUrl = APIConfiguration.getFileDownloadPath(
				mCustomEntity.fileId, mCustomEntity.fileHash);
		return mCustomEntity;
	}

}
